import { Component, OnInit, Injector, ViewChild, Output, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    CodeItemServiceProxy,
    Base_DataItemDetailServiceProxy,
    ComboboxItemDto,
    SystemInfoEditDto
} from '@shared/service-proxies/stock-service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
@Component({
    selector: 'createOrEditSystemInfoModal',
    templateUrl: './create-or-edit-systeminfo-modal.component.html'
})
export class CreateOrEditSystemInfoModalComponent extends AppComponentBase implements OnInit {
    constructor(
        injector: Injector,
        private _codeitemServiceProxy: CodeItemServiceProxy,
        private _dataItemDetailService: Base_DataItemDetailServiceProxy
    ) {
        super(injector);
    }
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    active = false;
    saving = false;
    systeminfo_id: string;
    value: string;
    systeminfo: SystemInfoEditDto = new SystemInfoEditDto();
    deptitems: ComboboxItemDto[];
    ngOnInit(): void {
        this._dataItemDetailService.getComboxDataitems('ruleType').subscribe(result => {
            this.deptitems = result;
        });
    }

    show(): void {
        const self = this;
        self.active = true;
        self.systeminfo_id = '';
        self.systeminfo = new SystemInfoEditDto();
        self.modal.show();
    }

    showupdate(event): void {
        const self = this;
        self.active = true;
        self.systeminfo_id = event.id;
        self.systeminfo = event;
        self.modal.show();
    }

    onShown(): void {
        //   document.getElementById('f_ProductName').focus();
    }

    save(): void {
        this.saving = true;
        let re;
        this._codeitemServiceProxy
            .getTableName(this.systeminfo.id, this.systeminfo.f_SystemCode)
            .pipe(
                finalize(() => {
                    if (re == 'true') {
                        this.message.warn('系统编码不允许重复');
                        this.saving = false;
                        return;
                    } else {
                        this._codeitemServiceProxy
                            .createOrUpdateSystemInfoAsync(this.systeminfo)
                            .pipe(
                                finalize(() => {
                                    this.saving = false;
                                    this.modalSave.emit(null);
                                })
                            )
                            .subscribe(() => {
                                this.notify.info(this.l('SavedSuccessfully'));
                                this.close();
                                this.modalSave.emit(null);
                            });
                    }
                })
            )
            .subscribe(result => {
                if (!result) {
                    re = 'false';
                } else {
                    re = 'true';
                }
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
